import { _decorator, Component, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TallentButton')
export class TallentButton extends Component {
    private _cb: Function;
    private _target: any;

    private _isActivated: boolean = true; // 是否激活
    public get isActivated(): boolean {
        return this._isActivated;
    }
    public set isActivated(value: boolean) {
        this._isActivated = value;

        // 置灰
        this.node.getComponentsInChildren(Sprite).forEach((s:Sprite)=> {
            s.grayscale = !value;
        })
    }


    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this, true);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this, true);
    }

    protected onDisable(): void {
        
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this, true);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this, true);
    }


    start() {

    }

    update(deltaTime: number) {
        
    }

    onTouchStart(){
        if(!this.isActivated) return;
        this.node.setScale(0.9,0.9);
    }

    onTouchEnd(){
        if(!this.isActivated) return;
        this.node.setScale(1,1);

        this._cb && this._cb.apply(this._target);
    }

    onTouchCancel(){
        if(!this.isActivated) return;
        this.node.setScale(1,1)
    }

    onClick(cb: Function, target?:any) {
        this._cb = cb;
        this._target = target;
    }
}


